package com.shuoyue.ycgk.ui.news;

import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentAdapter;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.News;
import com.shuoyue.ycgk.entity.NewsType;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.course.FragmengCourseItem;
import com.shuoyue.ycgk.ui.news.contract.NewsContract;
import com.shuoyue.ycgk.views.TabLayoutUtil;
import com.shuoyue.ycgk.views.viewpager_scroll.SuperViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FragmentNewsTab extends BaseMvpFragment<NewsContract.Presenter>
        implements NewsContract.View {


    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    SuperViewPager viewPager;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;


    List<BaseMvpFragment> fragmentMainItems;
    Type baseType;
    IndexFragmentStateAdapter indexFragmentStateAdapter;
    //    VipContract.Presenter comPresenter;
    @Override
    public void initParams(Bundle bundle) {
        baseType = (Type) bundle.getSerializable("questionType");

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_news_tab;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
//        setQuestionType(getTest());
        mPresenter = new NewsContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getNewsType(baseType.getId());

    }

    public static FragmentNewsTab getInstance(Type type) {
        FragmentNewsTab fragment = new FragmentNewsTab();
        Bundle bundle = new Bundle();
        bundle.putSerializable("questionType", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void setType(List<NewsType> mTypes) {
        fragmentMainItems = new ArrayList<>();
        for (NewsType topType : mTypes) {
            fragmentMainItems.add(FragmentNewsItem.getInstance(baseType, topType));
        }
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        indexFragmentStateAdapter = new IndexFragmentStateAdapter(getChildFragmentManager(),
                fragmentMainItems);
        viewPager.setAdapter(indexFragmentStateAdapter);
        viewPager.setCurrentItem(0);
        //设置成功了,把刷新禁止了
        refreshlayout.setEnableRefresh(false);
        TabLayoutUtil tabLayoutUtil = new TabLayoutUtil();
        tabLayoutUtil.setTabTextSizeWhenSelect(tabLayout, mTypes);
    }


    @Override
    public void setNewsInfo(News newsInfo) {

    }

    @Override
    public void setNewsList(ListWithPage<News> newsList) {

    }

    @Override
    public void setNewsTypeList(List<NewsType> newsTypeList) {
        setType(newsTypeList);
    }


    public NewsType getCurrentType() {
        if (indexFragmentStateAdapter != null
                && indexFragmentStateAdapter.getCurrentFragment() != null) {
            FragmentNewsItem fragmentNewsItem
                    = (FragmentNewsItem) indexFragmentStateAdapter.getCurrentFragment();
            return  fragmentNewsItem.topType;
        }
        return null;
    }
}
